package com.stone.sys.service;

/**
 * <pre>
 *  机构Service
 * </pre>
 */

import java.util.HashMap;
import java.util.List;

import com.stone.sys.pojo.TOrgan;

import frm.ssh.exception.AppException;
import frm.ssh.service.BaseService;

public class OrganService extends BaseService {

	/**
	 * 	新建、修改机构
	 */
	public void save(TOrgan organ) throws AppException {
		
		if (organ.getId() == null || organ.getId() == 0) {
			
			TOrgan o = (TOrgan) super.findById(organ.getParentId(), TOrgan.class.getName());
			
			List<TOrgan> list = super.findListByHql(" from TOrgan where parentId = " + organ.getParentId() + " order by organCode desc");
			
			if (list.size() > 0) {
				organ.setOrganCode(list.get(0).getOrganCode() + 1);
				organ.setParentCode(o.getOrganCode());
			} else {
				
				if(organ.getParentId() != 0) {
					organ.setOrganCode(Long.parseLong(String.valueOf(o.getOrganCode()) + "001"));
					organ.setParentCode(o.getOrganCode());
				} else {
					organ.setOrganCode(Long.parseLong(String.valueOf("101")));
					organ.setParentCode(Long.parseLong("0"));
				}
			}
			 
			// 取得ID
		//	organ.setId(SeqUtil.getNextKey("SEQ_SYS"));
			super.save(organ);
		} else {
			
			TOrgan u = (TOrgan) super.findById(organ.getId(), TOrgan.class.getName());
			u.setOrganName(organ.getOrganName());
			u.setTelephonenumber(organ.getTelephonenumber());
			u.setDescription(organ.getDescription());
			u.setOrganNameEn(organ.getOrganNameEn());
			// 将页面提交的实体复制至u
			//MainUtils.copy(organ, u);
			super.update(u);
		}
	}
	
	/**
	 */
	public void delete(Long id) throws AppException {
		super.delete(id, "TOrgan");
	}

	/**
	 * 执行Hql查询查询
	 * 
	 * @param hql  查询语句
	 * @return List 结果集
	 */
	public List findListByHql(String hql)  {
			return super.findListByHql(hql);
	}
	
	
	/**
	 * 根据ID查询记录
	 * 
	 * @param id ID
	 * @param objectFullName POJO名 包括包路径
	 * @return Object
	 */
	public Object findById(Long id, String objectFullName)  {
		return super.findById(id, objectFullName);
	}
	
	
	/**
	 * 保存pojo对象
	 * 
	 * @param transientInstance pojo对象
	 */
	public void save(Object transientInstance)  {
			super.save(transientInstance);
	}
	/**
	 * 修改记录
	 * 
	 * @param transientInstance pojo对象
	 */
	public void update(Object transientInstance)  {
			super.update(transientInstance);
	}
	
	/**
	 * 修改记录
	 * 
	 * @param transientInstance pojo对象
	 */
	public void doDelAll(final String ids,final String objectSimpleName)  {
			super.doDelAll(ids,objectSimpleName);
	}
	
	/**
	 * 通过SQL查询列表，返回每条是一个HashMap<String,Object>
	 * @param sql 查询SQL语句
	 * @return list 返回列表
	 */
	public List<HashMap<String,Object>> findListBySql(String sql){
	
		List<HashMap<String,Object>> list =  (List<HashMap<String,Object>>) super.findListBySql(sql);
		return list;
	}

	
	/**
	 * 此方法是用来动态获取列表，可以多表关联
	 * 
	 * @param cols 查询列名
	 * @param conditions 查询条件
	 * @param vals 查询条件对应值
	 * @return clause order by, group by,having等子句
	 * @param objectSimpleName 
	 */
	public List getListByDHql(final String cols, final String conditions,
			final Object[] vals, final String clause,
			final String objectSimpleName)  {
		
		return super.getListByDHql(cols, conditions, vals, clause, objectSimpleName);
	}
	
	/**
	 * 根据ID删除记录
	 * 
	 * @param id 删除记录的ID
	 * @param objectSimpleName
	 */
	public void delete(Long id, String objectSimpleName){
			super.delete(id, objectSimpleName);
	}
	/**
	 * 通过HQL修改记录
	 * 
	 * @param hql 修改的HQL语句
	 * @param values HQL语句对应值
	 */
	public void updateByHQL(String hql, Object[] values){
		super.updateByHQL(hql,values);
	}

	/**
	 * 查看部门是否产生业务
	 * @param o
	 * @return
	 */
	public long getCountByOrgan(TOrgan o) {
		
		long count = 0;
		
		// 人员
		count = super.getTotalByDHql("organ.id = ?", new Object[]{o.getId()}, "TUser");
		
		if (count > 0) {
			return count;
		};

		
		return count;
		
	}

}
